<?php
/**
 * Created by PhpStorm.
 * User: 魏爷
 * Date: 2016/4/22
 * Time: 12:57
 */

namespace Admin\Controller;


class SystemManageController extends BaseController
{
    /**
     * 用户信息管理
     **/
    public function userManage(){
        $a_id=getAdminId();
        $user_list=D('SystemManage')->getAdminInfo($a_id);
        $this->assign('_adminList',$user_list);
        $this->display('SystemManage/userManage');
    }
    /**
     * 图书馆管理
    **/
    public function libraryManage(){
        $getLibraryInfo=D('Library')->getLibraryInfo();
        $this->assign('_getLibraryInfo',$getLibraryInfo);
        $this->display('SystemManage/libraryManage');
    }
    /**
     * 管理员管理
     **/
    public function adminManage(){
        $a_email=I('a_email');
        $a_status=I('a_status');
        $getAdminCategoryInfo=D('AdminLogin')->getAdminCategoryInfo($a_email,$a_status);
        $this->assign('_getAdminCategoryInfo',$getAdminCategoryInfo);
        $this->display('SystemManage/adminManage');
    }
    /**
     * 图书分类管理
     **/
    public function bookClassManage(){
        $bookCaseInfo=D('BookCase')->getBookCaseInfo();
        $bookIsbnInfo=D('BookIsbn')->getBookIsbnInfo();
        $bookTypeInfo=D('BookType')->getBookTypeInfo();
        $this->assign('_bookCaseInfo',$bookCaseInfo);
        $this->assign('_bookIsbnInfo',$bookIsbnInfo);
        $this->assign('_bookTypeInfo',$bookTypeInfo);
        $this->display('SystemManage/bookClassManage');
    }
    /**
     * 登陆密码
     **/
    public function loginPassword(){
        $this->display('SystemManage/loginPassword');
    }

    /***图书分类管理***/
    public function  bookClassManageAdd(){
        $id=I('id');
        $a_id=getAdminId();
        switch($id){
            case 1:
                $book_case_name=I('book_case_name');
                if(empty($book_case_name)||empty($a_id)){
                    $this->error('添加失败，请重新添加');
                }
                $book_name=D('BookCase')->getBookCaseName($book_case_name);
                if($book_name==false){
                    $this->error('书架名称已存在，请重新输入');
                }
                $res=D('BookCase')->bookCaseAdd($a_id,$book_case_name);
                if($res==false){
                    $this->error('添加失败，请重新添加');
                }else{
                    $this->success('添加成功',U('Admin/SystemManage/bookClassManage'));
                }
                break;
            case 2:
                $book_type_name=I('book_type_name');
                if(empty($book_type_name)||empty($a_id)){
                    $this->error('添加失败，请重新添加');
                }
                $type_name=D('BookType')->getBookTypeName($book_type_name);
                if($type_name==false){
                    $this->error('类型名称已存在，请重新输入');
                }
                $res=D('BookType')->bookTypeAdd($a_id,$book_type_name);
                if($res==false){
                    $this->error('添加失败，请重新添加');
                }else{
                    $this->success('添加成功',U('Admin/SystemManage/bookClassManage'));
                }
                break;
            case 3:
                $book_ISBN_name=I('book_ISBN_name');
                if(empty($book_ISBN_name)||empty($a_id)){
                    $this->error('添加失败，请重新添加');
                }
                $Isbn_name=D('BookIsbn')->getBookIsbnName($book_ISBN_name);
                if($Isbn_name==false){
                    $this->error('出版社名称已存在，请重新输入');
                }
                $res=D('BookIsbn')->bookIsbnAdd($a_id,$book_ISBN_name);
                if($res==false){
                    $this->error('添加失败，请重新添加');
                }else{
                    $this->success('添加成功',U('Admin/SystemManage/bookClassManage'));
                }
                break;
            default:
                break;
        }
    }
    /**
     * 书架删除
    **/
    public function bookCaseDel(){
        $book_case_id=I('book_case_id');
        $res=D('BookCase')->bookCaseDel($book_case_id);
        if($res==false){
            $this->error('禁用失败',null,true);
        }else{
            $this->success('禁用成功',null,true);
        }
    }
    /**
     * 书架恢复
     **/
    public function bookCaseRec(){
        $book_case_id=I('book_case_id');
        $res=D('BookCase')->bookCaseRec($book_case_id);
        if($res==false){
            $this->error('恢复失败',null,true);
        }else{
            $this->success('恢复成功',null,true);
        }
    }
    /**
     * 出版社删除
     **/
    public function bookIsbnDel(){
        $book_isbn_id=I('book_isbn_id');
        $res=D('BookIsbn')->bookIsbnDel($book_isbn_id);
        if($res==false){
            $this->error('禁用失败',null,true);
        }else{
            $this->success('禁用成功',null,true);
        }
    }
    /**
     * 出版社恢复
     **/
    public function bookIsbnRec(){
        $book_isbn_id=I('book_isbn_id');
        $res=D('BookIsbn')->bookIsbnRec($book_isbn_id);
        if($res==false){
            $this->error('恢复失败',null,true);
        }else{
            $this->success('恢复成功',null,true);
        }
    }
    /**
     * 图书类型删除
     **/
    public function bookTypeDel(){
        $book_type_id=I('book_type_id');
        $res=D('BookType')->bookTypeDel($book_type_id);
        if($res==false){
            $this->error('禁用失败',null,true);
        }else{
            $this->success('禁用成功',null,true);
        }
    }
    /**
     * 图书类型恢复
     **/
    public function bookTypeRec(){
        $book_type_id=I('book_type_id');
        $res=D('BookType')->bookTypeRec($book_type_id);
        if($res==false){
            $this->error('恢复失败',null,true);
        }else{
            $this->success('恢复成功',null,true);
        }
    }
    /**
     * 图书馆界面管理 library
    **/
    public function librarySaveInfo(){
        $library_top_title=I('library_top_title');
        $library_title=I('library_title');
        $library_search_book=I('library_search_book');
        $library_title_left=I('library_title_left');
        $library_content_left=I('library_content_left');
        $library_title_right=I('library_title_right');
        $library_content_right=I('library_content_right');
        $library_title_center=I('library_title_center');
        $library_content_center=I('library_content_center');
        $library_bottom_content=I('library_bottom_content');
        $library_copyright=I('library_copyright');
        $library_bottom_title=I('library_bottom_title');
        if(empty($library_top_title)||empty($library_title)||empty($library_search_book)||empty($library_title_left)||empty($library_content_left)||empty($library_title_right)||empty($library_content_right)||empty($library_title_center)||empty($library_content_center)||empty($library_bottom_content)||empty($library_copyright)||empty($library_bottom_title)){
            $this->error('信息不能为空',U('Admin/SystemManage/libraryManage'));
        }
        $res=D('Library')->librarySaveInfo($library_top_title,$library_title,$library_search_book,$library_title_left,$library_content_left,$library_title_right,$library_content_right,$library_title_center,$library_content_center,$library_bottom_content,$library_copyright,$library_bottom_title);
        if($res==false){
            $this->error('修改失败',U('Admin/SystemManage/libraryManage'));
        }else{
            $this->success('修改成功',U('Admin/SystemManage/libraryManage'));
        }
    }
    /**
     * 管理员禁用
    **/
    public function adminTypeDel(){
        $a_id=I('a_id');
        $res=D('AdminLogin')->adminTypeDel($a_id);
        if($res==false){
            $this->error('禁用失败',null,true);
        }else{
            $this->success('禁用成功',null,true);
        }
    }
    /**
     * 管理员恢复
     **/
    public function adminTypeRec(){
        $a_id=I('a_id');
        $res=D('AdminLogin')->adminTypeRec($a_id);
        if($res==false){
            $this->error('恢复失败',null,true);
        }else{
            $this->success('恢复成功',null,true);
        }
    }
    /**
     * 管理员修改跳转
    **/
    public function adminInfoSaveTiao(){
        $a_id=I('a_id');
        if(empty($a_id)){
            $a_id=getAdminSaveId();
            $getAdminInfoById=D('AdminLogin')->getAdminInfoById($a_id);
            $this->assign('_getAdminInfoById',$getAdminInfoById);
            $this->display('SystemManage/adminSaveManage');
        }else{
            session('a_save_id',$a_id);
            $getAdminInfoById=D('AdminLogin')->getAdminInfoById($a_id);
            $this->assign('_getAdminInfoById',$getAdminInfoById);
            $this->display('SystemManage/adminSaveManage');
        }
    }
    /**
     * 管理员修改
    **/
    public function adminSaveManage(){
        $a_id=getAdminSaveId();
        $a_name=I('a_name');
        $a_tel=I('a_tel');
        if(empty($a_name)||empty($a_tel)||empty($a_id)){
            $this->error('信息不能为空',U('Admin/SystemManage/adminInfoSaveTiao'));
        }
        $res=D('AdminAdd')->adminSaveManage($a_id,$a_name,$a_tel);
        if($res==false){
            $this->error('修改失败',U('Admin/SystemManage/adminInfoSaveTiao'));
        }else{
            $this->success('修改成功',U('Admin/SystemManage/adminInfoSaveTiao'));
        }
    }
    /***用户管理修改***/
    public function userManageSave(){
        $a_name=I('a_name');
        $a_email=I('a_email');;
        $a_tel=I('a_tel');
        $a_department=I('a_department');
        if(empty($a_name)||empty($a_tel)||empty($a_department)||empty($a_email)){
            $this->error('请完善信息！');
        }
        $res=D('SystemManage')->userManageSave($a_name,$a_email,$a_tel,$a_department);
        if($res==false){
            $this->error('修改失败，请重新填写！');
        }else{
            $this->success('修改成功！',U('Admin/SystemManage/userManage'));
        }
    }
    /**登陆密码修改**/
    public function loginPasswordSave(){
        $a_pwd=I('a_pwd');
        $a_pwd_new=I('a_pwd_new');
        $a_repwd_new=I('a_repwd_new');
        if(empty($a_pwd)||empty($a_pwd_new)||empty($a_repwd_new)){
            $this->error('输入的信息不能为空！');
        }
        if($a_pwd_new!=$a_repwd_new){
            $this->error('新生密码与重复密码不一致，请重新输入！');
        }
        $a_id=getAdminId();
        $a_info=D('SystemManage')->getAdminInfo($a_id);
        if($a_info['a_pwd']!=myMd5($a_pwd)){
            $this->error('请输入正确的原始密码！');
        }
        $res=D('SystemManage')->loginPasswordSave($a_pwd_new);
        if($res==false){
            $this->error('修改失败，请重新输入！');
        }else{
            $this->success('修改成功',U('Admin/SystemManage/loginPassword'));
        }
    }
}